package com.ft.adbix.core.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.ft.adbix.core.R;
import com.ft.adbix.core.ad.manager.InterstitialAdManager;
import com.ft.adbix.core.ad.manager.RewardAdManager;
import com.ft.adbix.core.base.BaseActivity;
import com.ft.adbix.core.listener.InterstitialListener;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.core.view.InterstitialPicAdView;
import com.ft.adbix.utils.AdChecker;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.AdOpener;
import com.ft.adbix.utils.AdTrackerReporter;
import com.ft.adbix.utils.ThreadUtils;

public class InterstitialActivity extends BaseActivity {

    private static String AD_REQ_ID = "ad_req_id";
    private String currentReqId = "";
    FrameLayout layoutContainer;
    Ad adData;
    private boolean isFullScreen = false;
    private InterstitialListener interstitialListener;
    private AdTrackerReporter reporter;
    private AdOpener adOpener;

    public static void start(Context context, String reqId) {
        Intent starter = new Intent(context, InterstitialActivity.class);
        starter.putExtra(AD_REQ_ID, reqId);
        context.startActivity(starter);
    }

    @Override
    public boolean isFullScreen() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_interstitial;
    }

    @Override
    public void initView() {
        layoutContainer = findViewById(R.id.adbix_interstitial_fl_container);
        loadAdData();
        if (adData == null) {
            finish();
            return;
        }
        interstitialListener = InterstitialAdManager.getInstance().getInterstitialListener(currentReqId);
        reporter = new AdTrackerReporter(adData);
        adOpener = new AdOpener(this, adData, reporter);
        if (isFullScreen) {
            if (AdChecker.videoEnable(adData)) {
                AdLogUtils.d("fullscreen video");
                //插全屏视频
                FullScreenVideoActivity.start(this, currentReqId);
                layoutContainer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                },1000);
            } else {
                AdLogUtils.d("fullscreen image");
                //全屏图片
                InterstitialPicAdView adView = new InterstitialPicAdView(InterstitialActivity.this, InterstitialPicAdView.INTERSTITIAL_STYLE_PIC);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutContainer.addView(adView, params);
                adView.renderView(adData, interstitialListener);
            }
        } else {
            //半屏
            if (AdChecker.videoEnable(adData)) {
                //半屏视频
                HalfScreenVideoActivity.start(this, currentReqId);
                layoutContainer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                },1000);
            } else {
                //半屏图片
                InterstitialPicAdView adView = new InterstitialPicAdView(InterstitialActivity.this, InterstitialPicAdView.INTERSTITIAL_STYLE_TOP_PIC);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                params.gravity = Gravity.CENTER;
                layoutContainer.addView(adView, params);
                adView.renderView(adData, interstitialListener);
            }
        }

    }

    private void loadAdData() {
        Intent intent = getIntent();
        currentReqId = intent.getStringExtra(AD_REQ_ID);
        adData = InterstitialAdManager.getInstance().getInterstitialAdData(currentReqId);
        AdLogUtils.d("interstitial ad load success = "+currentReqId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}